package com.mlm.mlmapp.adapter.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.util.Constants;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.activity.CouponGetListActivity_;
import com.mlm.mlmapp.adapter.view.listener.DresserDetailHeaderSelectedListener;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.utils.CommonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

@SuppressLint("ValidFragment")
public class HairDresserDetailHeaderFragment extends Fragment {
	AQuery aq;
	private Context context;
	ImageView iv_headimg;
	TextView tv_name;
	TextView tv_post;
	TextView tv_words;
	TextView tv_orders;
	TextView tv_type;
	TextView tv_collections;
	TextView tv_price;
	TextView tv_awards;
	TextView tv_adept;
	LinearLayout ll_evalute_list;
	// RatingBar ratingBar1;
	Button btn_appointment;
	Button btn_collect;
	Button btn_coupon;
	Button btn_evaluate;
	String id;
	JSONArray comments;
	DresserDetailHeaderSelectedListener l;
	String mobile;
	int isCollect;

	public HairDresserDetailHeaderFragment(Context con,
			DresserDetailHeaderSelectedListener l, String mobile) {

		this.context = con;
		this.l = l;
		this.mobile = mobile;
	}

	public void setCollect(boolean status) {
		if (status) {
			isCollect = 2;
			btn_collect.setText("未收藏");
			btn_collect.setCompoundDrawablesWithIntrinsicBounds(getContext()
					.getResources().getDrawable(R.drawable.collectiondef),
					null, null, null);
		} else {
			isCollect = 1;
			btn_collect.setText("已收藏");
			btn_collect.setCompoundDrawablesWithIntrinsicBounds(getContext()
					.getResources().getDrawable(R.drawable.collectsel), null,
					null, null);
		}
	}

	public void setCounts(int count) {
		tv_collections.setText(context
				.getString(R.string.txt_collection, count));
	}

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.hairdress_detail_header,
				container, false);
		if (aq != null) {
			aq = aq.recycle(view);
		}
		aq = new AQuery(view);

		iv_headimg = (ImageView) view.findViewById(R.id.iv_headimg);

		tv_name = (TextView) view.findViewById(R.id.tv_name);
		tv_post = (TextView) view.findViewById(R.id.tv_post);
		tv_words = (TextView) view.findViewById(R.id.tv_words);
		tv_orders = (TextView) view.findViewById(R.id.tv_orders);
		tv_type = (TextView) view.findViewById(R.id.tv_type);
		tv_collections = (TextView) view.findViewById(R.id.tv_collections);
		tv_price = (TextView) view.findViewById(R.id.tv_price);
		tv_awards = (TextView) view.findViewById(R.id.tv_awards);
		tv_adept = (TextView) view.findViewById(R.id.tv_adept);
		ll_evalute_list = (LinearLayout) view
				.findViewById(R.id.ll_evalute_list);
		// ratingBar1 = (RatingBar) view.findViewById(R.id.ratingBar1);
		btn_appointment = (Button) view.findViewById(R.id.btn_appointment);
		btn_collect = (Button) view.findViewById(R.id.btn_collect);
		btn_coupon = (Button) view.findViewById(R.id.btn_coupon);
		btn_evaluate = (Button) view.findViewById(R.id.btn_evaluate);

		Bundle b = getArguments();

		try {
			JSONObject json = new JSONObject(b.getString(HTTPCons.JSON));
			tv_name.setText(CommonUtils.getJsonString(json, "name"));
			tv_post.setText(CommonUtils.getJsonString(json, "leveName"));
			tv_words.setText(CommonUtils.getJsonString(json, "introduce"));

			tv_orders.setText(context.getString(R.string.txt_order,
					CommonUtils.getJsonInt(json, "bought")));
			tv_collections.setText(context.getString(R.string.txt_collection,
					CommonUtils.getJsonInt(json, "collected")));
			tv_price.setText(context.getString(R.string.txt_price,
					CommonUtils.getJsonInt(json, "priceMast")));
			tv_awards.setText(CommonUtils.getJsonString(json, "awards"));
			tv_adept.setText(CommonUtils.getJsonString(json, "adept"));
			isCollect = CommonUtils.getJsonInt(json, "isCollect");
			if (isCollect == 1) {
				btn_collect.setText("已收藏");

				btn_collect.setCompoundDrawablesWithIntrinsicBounds(
						getContext().getResources().getDrawable(
								R.drawable.collectsel), null, null, null);
			} else {
				btn_collect.setText("未收藏");
				btn_collect.setCompoundDrawablesWithIntrinsicBounds(
						getContext().getResources().getDrawable(
								R.drawable.collectiondef), null, null, null);
			}
			btn_evaluate.setText(context.getString(R.string.evalute_all,
					CommonUtils.getJsonInt(json, "userTotal")));

			// ratingBar1.setRating(json.getInt("starLevel"));

			aq.id(iv_headimg).image(
					CommonUtils.getJsonString(json, "iconPhotoUrl"), true,
					true, 200, 0, null, Constants.FADE_IN);
			comments = CommonUtils.getJsonArray(json, "userComments");
			if (comments != null && comments.length() > 0) {
				btn_evaluate.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						l.onCommentsAllClick("");
					}
				});
			}

			btn_collect.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					l.onCollectClick(isCollect);
				}
			});
			btn_coupon.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					CouponGetListActivity_.intent(getContext()).start();
				}
			});
			btn_appointment.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					l.onAppointment("");
				}
			});

			if (comments != null && comments.length() > 0) {
				for (int i = 0; i < comments.length(); i++) {
					JSONObject obj = comments.getJSONObject(i);

					RelativeLayout child = (RelativeLayout) inflater.inflate(
							R.layout.hairdress_evalute_view, ll_evalute_list,
							false);

					TextView tv_name = (TextView) child
							.findViewById(R.id.tv_name);
					TextView tv_date = (TextView) child
							.findViewById(R.id.tv_date);
					TextView tv_evalute = (TextView) child
							.findViewById(R.id.tv_evalute);
					ImageView iv_headimg = (ImageView) child
							.findViewById(R.id.iv_headimg);
					tv_name.setText(CommonUtils.getJsonString(obj, "name"));
					tv_date.setText(CommonUtils.getJsonString(obj, "comDate"));
					tv_evalute.setText(CommonUtils
							.getJsonString(obj, "content"));

					aq.id(iv_headimg).image(
							CommonUtils.getJsonString(obj, "iconPhotoUrl"),
							true, true, 200, 0, null, Constants.FADE_IN);
					ll_evalute_list.addView(child);

					if (i < 2) {
						View v = new View(context);
						LayoutParams lp = new LayoutParams(
								LayoutParams.FILL_PARENT,
								LayoutParams.WRAP_CONTENT);
						lp.height = (int) context.getResources().getDimension(
								R.dimen.comments_padding);
						v.setLayoutParams(lp);
						ll_evalute_list.addView(v);
					}
				}

			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return view;
	}

}
